package com.media.publish.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;


import com.baselibrary.bean.ReleaseTopicBean;
import com.media.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 关忆北、
 * @description 发布界面推荐话题
 * @time 2022/4/14 10:52 上午
 */
public class RecoTopicAdapter extends RecyclerView.Adapter<RecoTopicAdapter.RecoTopicHolder> {
    private final Activity activity;
    private onItemClickListener mOnItemClickListener;
    private List<ReleaseTopicBean.DataBean> mSearchFriendsList = new ArrayList<>();
    private ReleaseTopicBean.DataBean selectBean;
    private TextView mLastView;
    private Drawable mDrawable = null;

    public RecoTopicAdapter(Activity activity) {
        this.activity = activity;
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @SuppressLint("NewApi")
    public void setData(List<ReleaseTopicBean.DataBean> list) {
        this.mSearchFriendsList = list;
        notifyDataSetChanged();
    }

    @SuppressLint("NewApi")
    public int upSelect(int selectPos) {
        int pos = -1;
        for (int i = 0; i < mSearchFriendsList.size(); i++) {
            if (selectPos == i) {//如果是当前选中的索引，则取消下面操作
                mSearchFriendsList.get(i).setSelect(true);
                pos = i;
                continue;
            }
            mSearchFriendsList.get(i).setSelect(false);
        }
        notifyDataSetChanged();
        return pos;
    }


    public int setSelectData(ReleaseTopicBean.DataBean bean) {
        int pos = -1;
        for (int i = 0; i < mSearchFriendsList.size(); i++) {
            if (bean.getId() == mSearchFriendsList.get(i).getId()) {
                mSearchFriendsList.get(i).setSelect(true);
                pos = i;
                continue;
            }
            mSearchFriendsList.get(i).setSelect(false);
            notifyDataSetChanged();
        }
        return pos;
    }

    @NonNull
    @Override
    public RecoTopicHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new RecoTopicHolder(activity.getLayoutInflater().inflate(R.layout.item_locationlist, viewGroup, false));
    }

    @SuppressLint({"UseCompatLoadingForDrawables", "SetTextI18n"})
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull RecoTopicHolder holder, int pos) {
        ReleaseTopicBean.DataBean dataBean = mSearchFriendsList.get(pos);
        holder.mTvRecoTopicName.setText("# " + dataBean.getName());
        mDrawable = activity.getDrawable(R.drawable.shape_publish_topic);
        if (dataBean.isSelect()) {
            Drawable mSelectDrawable = activity.getDrawable(R.drawable.shape_selectint_loaction);
            holder.mTvRecoTopicName.setBackground(mSelectDrawable);
            holder.mTvRecoTopicName.setTextColor(activity.getResources().getColor(R.color.white));
        } else {
            holder.mTvRecoTopicName.setBackground(mDrawable);
            holder.mTvRecoTopicName.setTextColor(activity.getResources().getColor(R.color.color_666666));
        }

        holder.itemView.setOnClickListener(view -> {
            if (mOnItemClickListener != null)
                mOnItemClickListener.onItemClick(dataBean);
            dataBean.setSelect(!dataBean.isSelect());
            upSelect(pos);
        });
    }

    @Override
    public int getItemCount() {
        return mSearchFriendsList.size();
    }


    public static class RecoTopicHolder extends RecyclerView.ViewHolder {

        private final TextView mTvRecoTopicName;

        public RecoTopicHolder(@NonNull View itemView) {
            super(itemView);
            mTvRecoTopicName = itemView.findViewById(R.id.tv_recoTopic_name);
        }
    }

    public interface onItemClickListener {
        void onItemClick(ReleaseTopicBean.DataBean dataBean);
    }
}
